<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

echo "\n";

echo count($cars);

echo "\n";
$arrlength = count($cars);

for($x=0;$x<$arrlength;$x++)
{
    echo $cars[$x];
//     echo "<br>";
    echo "\n";
}

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
echo "\n";

foreach($age as $x=>$x_value)
{
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "\n";
}


// 遍历数组

$fruits = array("苹果", "香蕉", "橙子", "葡萄");
$len = count($fruits);
for($x= 0;$x<$len;$x++){
        echo $fruits[$x]."\n";
}

echo "--------foreach-------\n";
foreach ($fruits as $value) {
        echo $value . "\n";
}

echo "--------foreach key value-------\n";
foreach ($age as $x => $x_value) {
        echo "Key=" . $x . ", Value=" . $x_value . "\n";
}

echo "--------array associative-------\n";
$person = array("name"=>"张三", "age"=>30, "city"=>"北京");
foreach ($person as $key => $value) {
        echo $key . " : " . $value . "\n";
}

echo "--------array multidimensional-------\n";
$people = array(
        array("name"=>"张三", "age"=>30, "city"=>"北京"),
        array("name"=>"李四", "age"=>25, "city"=>"上海"),
        array("name"=>"王五", "age"=>28, "city"=>"广州")
);
foreach ($people as $person) {
        echo "Name: " . $person['name'] . ", Age: " . $person['age'] . ", City: " . $person['city'] . "\n";
}

echo "----------array walk-------\n";
function print_value($value, $key)
{
    echo "Key=" . $key . ", Value=" . $value . "\n";
}
array_walk($age, 'print_value');
?>